package com.ldj.struts.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;

import com.ldj.hibernate.CaseInfoDAO;
import com.ldj.hibernate.EmployerInfo;
import com.ldj.hibernate.EmployerInfoDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.hibernate.VisiteInfo;
import com.ldj.hibernate.VisiteInfoDAO;
import com.ldj.struts.form.VisiteInfoForm;

public class VisiteInfoAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			VisiteInfoForm pForm = (VisiteInfoForm)form;
			String empInfoName = request.getParameter("empInfoName");
			VisiteInfoDAO dao = new VisiteInfoDAO(request, response);
			List list = dao.findAllPage(SystemSession.getSupUserId(request), empInfoName, pForm);
			request.setAttribute("list", list);
			request.setAttribute("intPage", String.valueOf(dao.intPage));
			return mapping.findForward("list");
	}
	
	public ActionForward inteStatList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			VisiteInfoForm pForm = (VisiteInfoForm)form;
			String empInfoName = request.getParameter("empInfoName");
			VisiteInfoDAO dao = new VisiteInfoDAO(request, response);
			List list = dao.findVisiteInfoAllPage(empInfoName, pForm);
			request.setAttribute("list", list);
			return mapping.findForward("inteStatList");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			return mapping.findForward("toAdd");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		VisiteInfoForm pForm = (VisiteInfoForm)form;
		VisiteInfoDAO VisiteInfoDao = new VisiteInfoDAO(request, response);
		VisiteInfo info = new VisiteInfo();
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		 String year = df.format(new Date());
		 String visiteInfoCode = "001";
		    int code = VisiteInfoDao.findMaxCode(year) + 1;
		    if(code < 10)
		    	visiteInfoCode = "00" + code;
		    else if(code < 100)
		    	visiteInfoCode = "0" + code;
		    visiteInfoCode = year + visiteInfoCode;
		info.setPetitionInfoRegister(visiteInfoCode);
		
		
		info.setPetitionInfoTime(pForm.getDatePetitionInfoTime());
		info.setPetitionInfoMode(pForm.getPetitionInfoMode());
		info.setPetitionInfoType(pForm.getPetitionInfoType());
		info.setPetitionInfoIfGroupP(pForm.getPetitionInfoIfGroupP());
		if("1".equals(pForm.getPetitionInfoIfGroupP()))
			info.setPetitionInfoPNum("1");
		else
			info.setPetitionInfoPNum(pForm.getPetitionInfoPNum());
		info.setPetitionInfoSource(pForm.getPetitionInfoSource());
		info.setPetitionInfoResion(pForm.getPetitionInfoResion());
		info.setPetitionInfoGoonTime(pForm.getPetitionInfoGoonTime());
		info.setPetitionInfoDealMode(pForm.getPetitionInfoDealMode());
		info.setPetitionInfoCircs(pForm.getPetitionInfoCircs());
		info.setPetitionInfoCompany(pForm.getPetitionInfoCompany());
		EmployerInfoDAO eDao = new EmployerInfoDAO(request, response);
		EmployerInfo eInfo = eDao.findById(pForm.getPetitionInfoReportedUnit());
		info.setEmployerInfo(eInfo);
		info.setPetitionInfoContent(pForm.getPetitionInfoContent());
		info.setPetitionInfoDealResult(pForm.getPetitionInfoDealResult());
		info.setPetitionInfoCreateTime(new Date());
		info.setPetitionInfoDelOper("0");
		info.setPetitionInfoCreator(SystemSession.getSupUserId(request));
		info.setPetitionInfoScope(pForm.getPetitionInfoScope());
		VisiteInfoDao.save(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("0");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "VisiteInfo.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getPetitionInfoRegister()));
		logDao.save(s);
		return new ActionForward("/VisiteInfo.do?method=list", true);
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		VisiteInfoForm pForm = (VisiteInfoForm)form;
		VisiteInfoDAO VisiteInfoDao = new VisiteInfoDAO(request, response);
		VisiteInfo info = VisiteInfoDao.findById(request.getParameter("vid"));
		request.setAttribute("vInfo", info);
		return mapping.findForward("detail");
	}

	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		    VisiteInfoForm pForm = (VisiteInfoForm)form;
			VisiteInfoDAO dao = new VisiteInfoDAO(request, response);
			VisiteInfo info = dao.findById(request.getParameter("petitionInfoId"));
			request.setAttribute("vInfo", info);
			request.setAttribute("nowPage",request.getParameter("page"));
			return mapping.findForward("toUpdate");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		VisiteInfoForm pForm = (VisiteInfoForm)form;
		VisiteInfoDAO dao = new VisiteInfoDAO(request, response);
		VisiteInfo info = dao.findById(request.getParameter("vid"));
		info.setPetitionInfoTime(pForm.getDatePetitionInfoTime());
		info.setPetitionInfoMode(pForm.getPetitionInfoMode());
		info.setPetitionInfoType(pForm.getPetitionInfoType());
		info.setPetitionInfoIfGroupP(pForm.getPetitionInfoIfGroupP());
		if("1".equals(pForm.getPetitionInfoIfGroupP()))
			info.setPetitionInfoPNum("1");
		else
			info.setPetitionInfoPNum(pForm.getPetitionInfoPNum());
		info.setPetitionInfoSource(pForm.getPetitionInfoSource());
		info.setPetitionInfoResion(pForm.getPetitionInfoResion());
		info.setPetitionInfoGoonTime(pForm.getPetitionInfoGoonTime());
		info.setPetitionInfoDealMode(pForm.getPetitionInfoDealMode());
		info.setPetitionInfoCircs(pForm.getPetitionInfoCircs());
		info.setPetitionInfoCompany(pForm.getPetitionInfoCompany());
		EmployerInfoDAO eDao = new EmployerInfoDAO(request, response);
		EmployerInfo eInfo = eDao.findById(pForm.getPetitionInfoReportedUnit());
		info.setEmployerInfo(eInfo);
		info.setPetitionInfoContent(pForm.getPetitionInfoContent());
		info.setPetitionInfoDealResult(pForm.getPetitionInfoDealResult());
		info.setPetitionInfoCreateTime(new Date());
		info.setPetitionInfoCreator(SystemSession.getSupUserId(request));
		info.setPetitionInfoScope(pForm.getPetitionInfoScope());
			dao.merge(info);
			
			SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
			SysLogInfo s = new SysLogInfo();
			s.setPersionFile(SystemSession.getSupUserInfo(request));
			s.setSysLogInfoOperDate(new Date());
			s.setSysLogInfoOperType("1");
			s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
			s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "VisiteInfo.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getPetitionInfoRegister()));
			logDao.save(s);
			return new ActionForward("/VisiteInfo.do?method=list&pages="+request.getParameter("pages"), true);
		}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		VisiteInfoDAO dao = new VisiteInfoDAO(request, response);
		String[]str = request.getParameterValues("vid");
		List list = dao.findFromIdArray(str);
		String title="";
		if(list != null && list.size() > 0){
			for(int i = 0; i < list.size(); i ++){
				VisiteInfo info = (VisiteInfo)list.get(i);
				info.setPetitionInfoDelOper("1");
				title+=info.getPetitionInfoRegister()+",";
				dao.merge(info);
			}
		}
		
		if(title.length()>0){
			title=title.substring(0, title.length()-1);
		}
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("2");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "VisiteInfo.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record",title));
		logDao.save(s);
		return new ActionForward("/VisiteInfo.do?method=list", true);
	}
	
	
}